/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.hs.srm.admin.modules.sys.controller;

import java.util.HashMap;
import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.hs.srm.base.common.utils.Constant;
import com.hs.srm.base.modules.sys.entity.SysDeptEntity;
import com.hs.srm.base.modules.sys.service.SysDeptService;
import com.hs.srm.common.utils.R;

/**
 * 部门管理
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-06-20 15:23:47
 */
@RestController
@RequestMapping("/sys/dept")
public class SysDeptController extends AbstractController {
	@Autowired
	private SysDeptService sysDeptService;

	/**
	 * 删除
	 */
	@RequestMapping("/delete/{deptId}")
	@RequiresPermissions("sys:dept:delete")
	public R delete(@PathVariable("deptId") long deptId) {
		// 判断是否有子部门
		List<Long> deptList = this.sysDeptService.queryDetpIdList(deptId);
		if(deptList.size() > 0) {
			return R.error("请先删除子部门");
		}

		this.sysDeptService.deleteById(deptId);

		return R.ok();
	}

	/**
	 * 获取用户部门
	 * @return
	 */
	@RequestMapping("/getUserDept")
	public R getUserDept() {
		List<SysDeptEntity> deptList = this.sysDeptService.queryList(new HashMap<String, Object>());
		return R.ok(deptList);
	}

	/**
	 * 获取用户部门
	 * @return
	 */
	@RequestMapping("/getUserDeptList")
	public List<SysDeptEntity> getUserDeptList() {
		List<SysDeptEntity> deptList = this.sysDeptService.queryList(new HashMap<String, Object>());
		return deptList;
	}

	/**
	 * 上级部门Id(管理员则为0)
	 */
	@RequestMapping("/info")
	@RequiresPermissions("sys:dept:list")
	public R info() {
		long deptId = 0;
		if(this.getUserId().intValue() != Constant.SUPER_ADMIN) {
			List<SysDeptEntity> deptList = this.sysDeptService.queryList(new HashMap<String, Object>());
			Long parentId = null;
			for(SysDeptEntity sysDeptEntity : deptList) {
				if(parentId == null) {
					parentId = sysDeptEntity.getParentId();
					continue;
				}

				if(parentId.intValue() > sysDeptEntity.getParentId().longValue()) {
					parentId = sysDeptEntity.getParentId();
				}
			}
			deptId = parentId;
		}

		return R.ok().put("deptId", deptId);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{deptId}")
	@RequiresPermissions("sys:dept:info")
	public R info(@PathVariable("deptId") Long deptId) {
		SysDeptEntity dept = this.sysDeptService.selectById(deptId);
		return R.ok().put("dept", dept);
	}

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:dept:list")
	public R list() {
		List<SysDeptEntity> deptList = this.sysDeptService.queryList(new HashMap<String, Object>());
		return R.ok(deptList);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:dept:save")
	public R save(SysDeptEntity dept) {
		if(ObjectUtils.isEmpty(dept.getParentId())) {
			return R.error().put("msg", "上级组织机构不能为空");
		}
		this.sysDeptService.save(dept);
		return R.ok();
	}

	/**
	 * 选择部门(添加、修改菜单)
	 */
	@RequestMapping("/select")
	@RequiresPermissions("sys:dept:select")
	public R select() {
		List<SysDeptEntity> deptList = this.sysDeptService.queryList(new HashMap<String, Object>());

		// 添加一级部门
		if(this.getUserId().intValue() == Constant.SUPER_ADMIN) {
			SysDeptEntity root = new SysDeptEntity();
			root.setDeptId(0L);
			root.setName("一级组织机构");
			root.setParentId(-1L);
			root.setOpen(true);
			deptList.add(root);
		}

		return R.ok(deptList);
	}

	/**
	 * 跳转至编辑页面
	 * @param deptId
	 * @return
	 */
	@RequestMapping("/toEdit/{deptId}")
	public ModelAndView toEdit(@PathVariable("deptId") Long deptId) {
		SysDeptEntity dept = this.sysDeptService.selectById(deptId);
		ModelAndView modelAndView = new ModelAndView("modules/sys/dept/dept_edit");
		// 查询上级
		if(dept.getParentId().intValue() == 0) {
			dept.setParentName("一级组织机构");
		} else {
			dept.setParentName(this.sysDeptService.selectById(dept.getParentId()).getName());
		}
		modelAndView.addObject("dept", dept);
		return modelAndView;
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions(value = {"sys:dept:edit"}, logical = Logical.AND)
	public R update(SysDeptEntity dept) {
		if(ObjectUtils.isEmpty(dept.getParentId())) {
			return R.error().put("msg", "上级组织机构不能为空");
		}
		this.sysDeptService.edit(dept);

		return R.ok();
	}
}
